package cn.tedu.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

/**
 * @author Amos
 * @date 2022/5/10
 */

public class WordCountMain {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // 构建job作业对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, WordCountMain.class.getName());
        // 如果上传到服务器上运行，加上一句话
        job.setJarByClass(WordCountMain.class);

        // 第一步，加载数据，指定TextInputFormat
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job,
                new Path("hdfs://hadoop01:8020/test/input/wordcount.txt"));

        // 第二步，指定mapper
        job.setMapperClass(WordMapper.class);
        // 指定map的数据输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 第七步，指定reducer
        job.setReducerClass(WordReducer.class);
        // 指定reduce端的数据输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 第八步，指定TextOutputFormat，数据输出
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,
                new Path("hdfs://hadoop01:8020/test/output"));

        // 提交任务
        job.waitForCompletion(true);

    }
}
